package scut.facerecognition.controller;

import cn.dev33.satoken.stp.SaTokenInfo;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import scut.facerecognition.entity.dto.EmailDTO;
import scut.facerecognition.entity.dto.RegisterDTO;
import scut.facerecognition.entity.pojo.User;
import scut.facerecognition.result.Result;
import scut.facerecognition.service.UserService;


import java.io.IOException;

@RestController("userController")
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    //新用户注册
    @PostMapping("/register")
    public Result<String> register(@RequestParam("file") MultipartFile file,
                                   @RequestParam("email") String email,
                                   @RequestParam("password") String password,
                                   @RequestParam("nickname") String nickname,
                                   @RequestParam("sex") String sex,
                                   @RequestParam("birthday") String birthday,
                                   @RequestParam("signature") String signature,
                                   @RequestParam("location") String location,
                                   @RequestParam("school") String school,
                                   @RequestParam("hobby") String hobby) {
        return userService.register(file, email, password, nickname, sex, birthday, signature, location, school, hobby);
    }


    //用户登陆
    @PostMapping("/login")
    public Result<SaTokenInfo> login(@RequestBody RegisterDTO registerDTO) {
        return userService.login(registerDTO);
    }

    //用户关键操作人脸认证（需要先登陆）
    @PostMapping("/faceRecognition")
    public Result<String> faceRecognition(@RequestParam("file") MultipartFile file) {
        try {
            return userService.faceRecognition(file);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            return Result.error("人脸识别失败");
        }

    }

    //修改用户信息 需要登陆
    @PostMapping("/updateUserInfo")
    public Result<String> updateUserInfo(@RequestParam("file") MultipartFile file,
                                         @RequestParam("email") String email,
                                         @RequestParam("password") String password,
                                         @RequestParam("nickname") String nickname,
                                         @RequestParam("sex") String sex,
                                         @RequestParam("birthday") String birthday,
                                         @RequestParam("signature") String signature,
                                         @RequestParam("location") String location,
                                         @RequestParam("school") String school,
                                         @RequestParam("hobby") String hobby) {
        return userService.updateUserInfo(file, email, password, nickname, sex, birthday, signature, location, school, hobby);
    }

    //获取自己用户信息  需要登陆
    @GetMapping("/getUserInfo")
    public Result<User> getUserInfo() {
        return userService.getUserInfo();
    }

    //获取他人用户信息
    @PostMapping("/getOtherInfo")
    public Result<User> getOtherInfo(@RequestBody RegisterDTO registerDTO){
        return userService.getOtherInfo(registerDTO);
    }

    //邮箱发送验证码
    @PostMapping("/sendCode")
    public Result<String> sendCode(@RequestBody EmailDTO emailDTO) {
        return userService.sendCode(emailDTO);
    }

    //验证验证码是否正确
    @PostMapping("/verifyCode")
    public Result<String> verifyCode(@RequestBody EmailDTO emailDTO) {
        return userService.verifyCode(emailDTO);
    }
}
